package top.smallway.UI;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class MainPage extends JFrame {
    private boolean isDraging;
    private int xx, yy;

    public MainPage() {
        setSize(1280, 800);
        this.setUndecorated(true);
        this.setBackground(new Color(0, 0, 0, 0));
        setLocationRelativeTo(null);
        MyPanels myPanels = new MyPanels("src/main/resources/BACKGROUND.png");
        placeComponents(myPanels);
        add(myPanels);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                isDraging = true;
                xx = e.getX();
                yy = e.getY();
            }


            @Override
            public void mouseReleased(MouseEvent e) {
                isDraging = false;
            }
        });
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDraging) {
                    int left = getX();
                    int top = getY();
                    setLocation(left + e.getX() - xx, top + e.getY() - yy);
                }
            }
        });
    }

    private void placeComponents(MyPanels myPanels) {
        myPanels.setLayout(new BorderLayout());
        Panels panels = new Panels();
        myPanels.add(panels, BorderLayout.NORTH);

    }

    static class Panels extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            BufferedImage backgroundImage;
            try {
                backgroundImage = ImageIO.read(new File("src/main/resources/Sidebar_Header.png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            // 确保 JPanel 背景是透明的
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制 PNG 图像 (确保透明部分显示)
            if (backgroundImage != null) {
                g2d.drawImage(backgroundImage, 0, 0, null);
            }
        }
    }
}
